package com.ruoyi.kjy.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.domain.IndustryAcademiaCollaboration;
import com.ruoyi.kjy.domain.IntellectualProperty;
import com.ruoyi.kjy.service.IIndustryAcademiaCollaborationService;
import com.ruoyi.kjy.service.IIntellectualPropertyService;
import org.apache.catalina.util.URLEncoder;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-09
 */
@RestController
@RequestMapping("/intellectual-property")
public class IntellectualPropertyController extends BaseController {

    @Autowired
    private IIntellectualPropertyService intellectualPropertyService;


    @GetMapping("/list")
    public AjaxResult list(Long current, Long size, IntellectualProperty intellectualProperty) {
        QueryWrapper<IntellectualProperty> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(intellectualProperty);
        if (Objects.isNull(current)) {
            current = 1l;
        }
        if (Objects.isNull(size)) {
            size = 10l;
        }
        if (size >= 20l) {
            size = 20l;
        }
        return AjaxResult.success(intellectualPropertyService.page(new Page<>(current, size), queryWrapper));
    }


    @GetMapping("/listByUserId")
    public AjaxResult listByUserId(Long current, Long size, IntellectualProperty intellectualProperty) {
        QueryWrapper<IntellectualProperty> queryWrapper = new QueryWrapper<>();
        intellectualProperty.setUserId(SecurityUtils.getUserId());
        queryWrapper.setEntity(intellectualProperty);
        if (Objects.isNull(current)) {
            current = 1l;
        }
        if (Objects.isNull(size)) {
            size = 10l;
        }
        if (size >= 20l) {
            size = 20l;
        }
        return AjaxResult.success(intellectualPropertyService.page(new Page<>(current, size), queryWrapper));
    }


    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(intellectualPropertyService.getById(id));
    }


    @Log(title = "企业知识产权", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntellectualProperty intellectualProperty) {
        intellectualProperty.setUserId(SecurityUtils.getUserId());
        intellectualProperty.setStatus(0);
        boolean save = intellectualPropertyService.save(intellectualProperty);
        return toAjax(save);
    }

    @Log(title = "企业知识产权", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntellectualProperty intellectualProperty) {
        intellectualProperty.setUserId(SecurityUtils.getUserId());
        return toAjax(intellectualPropertyService.updateById(intellectualProperty));
    }

    @Log(title = "企业知识产权", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable(value = "ids") Long[] ids) {
        return toAjax(intellectualPropertyService.removeByIds(Arrays.asList(ids)));
    }


    @GetMapping("/export")
    public void export(HttpServletResponse response) {

        List<IntellectualProperty> list = intellectualPropertyService.list();


        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        String fileName = URLEncoder.DEFAULT.encode("知识产权.xlsx", StandardCharsets.UTF_8);
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);

        try {
            EasyExcel.write(response.getOutputStream(), IntellectualProperty.class).sheet("知识产权").doWrite(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
